﻿using System;

using Portal.Data.Entity;

namespace Portal.Data.Webparts
{
    public class DesignModeWebpartLoader : IWebpartLoader
    {
        #region IWebpartLoader Members

        private System.Web.UI.Page _page;

        public System.Web.UI.Page WebPage
        {
            set
            {
                _page = value;
            }
        }

        public System.Web.UI.Control Contain(Webpart webpart)
        {
            WebpartControl wpControl;
            try
            {
                wpControl = (WebpartControl)_page.LoadControl(webpart.Definition().Path);
            }
            catch (Exception ex)
            {
                wpControl = new WebpartControl();
                var ltr = new System.Web.UI.WebControls.Literal
                {
                    Text = "<div style='background-color:#ffffff;border:solid 1px red;color:red'>" + ex.Message + "</div>"
                };
                wpControl.Controls.Add(ltr);
            }
            wpControl.ViewMode = PageViewMode.Design;
            wpControl.Info = webpart;

            var container = new DragDropContainer();
            container.Add(wpControl);
            container.Title = wpControl.Config.Title;

            var menu = new WebpartMenu();
            menu.CSSClass = "web_part_menu";
            menu.Add(new MenuItem("Thiết lập cấu hình", "cmd_edit", "javascript:module_edit(" + webpart.ID + ")"));
            menu.Add(new MenuItem("Clone", "cmd_clone", "javascript:module_clone(" + webpart.ID + ")"));
            menu.Add(new MenuItem(" Xóa ", "cmd_delete", "javascript:module_delete(" + webpart.ID + ")"));

            container.Menu = menu;

            return container;
        }

        #endregion
    }
}
